package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.dto.SensitiveDto;
import com.heima.admin.entity.AdSensitive;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.IAdSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2021-05-18
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements IAdSensitiveService {

    @Override
    public ResponseResult findListByName(SensitiveDto dto) {
        //判断dto里的参数是否为空
        LambdaQueryWrapper<AdSensitive> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getName())){
            //如果不是，就设置查询条件
            query.like(AdSensitive::getSensitives,dto.getName());
        }
        //如果是空的，就直接查询

        IPage<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        IPage<AdSensitive> iPage = this.page(page, query);

        return new PageResponseResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public ResponseResult saveSensitive(AdSensitive sensitive) {
        //判断对象是否是空或者参数为空
        if (sensitive==null || StringUtils.isEmpty(sensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断数据库里是否有这个敏感词
        LambdaQueryWrapper<AdSensitive> query = new LambdaQueryWrapper<>();
        query.eq(AdSensitive::getSensitives,sensitive.getSensitives());
        AdSensitive one = this.getOne(query);
        if (one!=null){
            //如果有，就返回
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);

        }
        //否则就保存
        sensitive.setCreatedTime(new Date());
        this.save(sensitive);
        return ResponseResult.okResult(sensitive.getId());
    }

    @Override
    public ResponseResult updateSensitive(AdSensitive sensitive) {
        //判断参数
        if (sensitive==null || StringUtils.isEmpty(sensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断数据库里是否有这个敏感词
        LambdaQueryWrapper<AdSensitive> query = new LambdaQueryWrapper<>();
        query.eq(AdSensitive::getSensitives,sensitive.getSensitives());
        AdSensitive one = this.getOne(query);
        if (one!=null){
            //如果有，就返回
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        this.updateById(sensitive);
        return ResponseResult.okResult();
    }


}
